package edu.psu.collegerecommendationhelper.dataentry;

import weka.core.*;
import weka.core.converters.*;
import weka.core.converters.ConverterUtils.DataSource;

import java.io.*;
import java.net.URL;

import weka.core.Instances;
import weka.experiment.InstanceQuery;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.NominalToString;
import weka.filters.unsupervised.attribute.RemoveUseless;
import weka.filters.unsupervised.attribute.StringToWordVector;

public class WekaDBConnect {
//public class WekaDBConnect implements Runnable{

	public Instances retrieveTrainingData() {
		DataSource source = null;
		Instances dataFiltered = null;

		try {
			source = new DataSource("/home/cromvary/GIT_Uploads/college-recommendation-tool/CDH_WebDevelopment/config/2011data.arff");

			Instances raw = source.getDataSet();

			RemoveUseless nfilter = new RemoveUseless();
			nfilter.setInputFormat(raw);

			dataFiltered = Filter.useFilter(raw, nfilter);
		}
		catch (Exception e1) {
			e1.printStackTrace();
		}

			return dataFiltered;
		}

////////////////////////////////////////////////////////////////////////////////

/*
   public void loadSavedCSV() throws Exception {
	    // load CSV
	    CSVLoader loader = new CSVLoader();
	    loader.setSource(new File(fileLocator() + "2011data.csv"));
	    Instances data = loader.getDataSet();
	 
	    // save ARFF
	    ArffSaver saver = new ArffSaver();
	    saver.setInstances(data);
	    saver.setFile(new File(fileLocator() + "test1.arff"));
	    saver.writeBatch();

        try {
	       saveARFFintoDB(data);
        } catch (Exception e) {
           e.printStackTrace();
        }
   }

	   // load CSV
      CSVLoader loader = new CSVLoader();
System.out.println(fileLocator() + "2011data.csv");
      try {
         loader.setSource(new File(fileLocator() + "2011data.csv"));
      } catch (IOException e3) {
         e3.printStackTrace();
      }


      Instances data = null;
      try {
         data = loader.getDataSet();
      } catch (IOException e3) {
         e3.printStackTrace();
      }

      // save ARFF
      ArffSaver saver = new ArffSaver();
      saver.setInstances(data);


      try {
         saver.setFile(new File(fileLocator() + "test1.arff"));
      } catch (IOException e2) {
         e2.printStackTrace();
      }

      try {
         saver.setDestination(new File(fileLocator() + "test1.arff"));
      } catch (IOException e2) {
         e2.printStackTrace();
      }

      try {
         saver.writeBatch();
      } catch (IOException e2) {
         e2.printStackTrace();
      }

      try {
         saveARFFintoDB(data);
      } catch (Exception e) {
         e.printStackTrace();
      }

   }
*/

////////////////////////////////////////////////////////////////////////////////

	public void saveARFFintoDB(Instances data) throws Exception {
      // save arff into DB
      DatabaseSaver save = new DatabaseSaver();
      save.setUrl("jdbc:mysql://localhost:3306/CDHDatabase");
      save.setUser("cromvary");
      save.setPassword("9Kuskup1");
      save.setInstances(data);
      save.setRelationForTableName(false);
      save.setTableName("wekatable");
      save.connectToDatabase();
      save.writeBatch();

   }

/*
   public static void main(String[] args) {
      (new Thread(new WekaDBConnect() )).start();
   }

   @Override
   public void run() {
      Instances dataFiltered = retrieveTrainingData();

      try {
         saveARFFintoDB(dataFiltered);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
*/
}
